package com.xiyu.service.repository.material.order;

import com.xiyu.service.model.material.info.MaterialCategoryFetcher;
import com.xiyu.service.model.material.info.MaterialInfoFetcher;
import com.xiyu.service.model.material.order.MaterialTransferDetail;
import com.xiyu.service.model.material.order.MaterialTransferDetailFetcher;
import com.xiyu.service.model.material.order.MaterialTransferDetailTable;
import org.babyfish.jimmer.spring.repository.JRepository;

import java.math.BigDecimal;
import java.util.List;

public interface MaterialTransferDetailRepository extends JRepository<MaterialTransferDetail, Long> {
    MaterialTransferDetailTable materialTransferDetailTable = MaterialTransferDetailTable.$;

    List<MaterialTransferDetail> findByTransferId(Long transferId);

    default List<MaterialTransferDetail> findDetailByTransferId(Long transferId){
        return sql().createQuery(materialTransferDetailTable)
                .where(materialTransferDetailTable.transferId().eq(transferId))
                .select(materialTransferDetailTable.fetch(MaterialTransferDetailFetcher.$.allTableFields()
                        .material(MaterialInfoFetcher.$.allTableFields()
                                .category(MaterialCategoryFetcher.$.allTableFields())))
                ).execute();
    }

    default List<MaterialTransferDetail> findByDetailColumn(Long transferId,Long materialId, String batchNo,
                                                            String productionDate, String expireDate){
        return sql().createQuery(materialTransferDetailTable)
                .where(materialTransferDetailTable.transferId().eq(transferId))
                .where(materialTransferDetailTable.materialId().eq(materialId))
                .where(materialTransferDetailTable.batchNo().eq(batchNo))
                .where(materialTransferDetailTable.productionDate().eq(productionDate))
                .where(materialTransferDetailTable.expireDate().eq(expireDate))
                .select(materialTransferDetailTable)
                .execute();
    }

    default void updateOutNumberById(Long id, BigDecimal outNumber){
        sql().createUpdate(materialTransferDetailTable)
                .set(materialTransferDetailTable.outNumber(),materialTransferDetailTable.outNumber().plus(outNumber))
                .where(materialTransferDetailTable.id().eq(id))
                .execute();
    }

    default void updateInNumberById(Long id, BigDecimal inNumber){
        sql().createUpdate(materialTransferDetailTable)
                .set(materialTransferDetailTable.inNumber(),materialTransferDetailTable.inNumber().plus(inNumber))
                .where(materialTransferDetailTable.id().eq(id))
                .execute();
    }

    default long findUnOutCountByTransferId(Long transferId){
        return sql().createQuery(materialTransferDetailTable)
                .where(materialTransferDetailTable.transferId().eq(transferId))
                .where(materialTransferDetailTable.number().gt(materialTransferDetailTable.outNumber()))
                .select(materialTransferDetailTable.id().count())
                .fetchOne();
    }

    default long findUnInCountByTransferId(Long transferId){
        return sql().createQuery(materialTransferDetailTable)
                .where(materialTransferDetailTable.transferId().eq(transferId))
                .where(materialTransferDetailTable.number().gt(materialTransferDetailTable.inNumber()))
                .select(materialTransferDetailTable.id().count())
                .fetchOne();
    }
}